#include <myheader_c_plus.h>
using std::cout;
using std::endl;

//begin of the Singleton
class Singleton{
public:
    static Singleton * getInstance(){
        if(_pInstance == nullptr){
            _pInstance = new Singleton();
            return _pInstance;
        }
        return _pInstance;
    }
    
private:

    //begin of the AutoRealse
    //自动类私有，并在单例类销毁时通过自动释放类的析构进行回收，设置为静态
    class AutoRelease{
    public:
        AutoRelease()
        {
        }

        ~AutoRelease(){
            if(_pInstance != nullptr){
                delete _pInstance;
                _pInstance = nullptr;
            }
        }
    private:

    };
    //end of the AutoRealse
    static AutoRelease _ar;
    static Singleton * _pInstance;
    //析构函数和构造函数私有，外部不能随便调用
    Singleton(){}
    ~Singleton(){}


};
//静态成员必须在类外进行声明
Singleton * Singleton::_pInstance = nullptr;
Singleton::AutoRelease Singleton:: _ar;
int main(int argc, char *argv[])
{
    Singleton * psingle = Singleton::getInstance();
    return 0;
}

